package com.ford.trade.processor;

import com.ford.trade.entity.Game;
import com.ford.trade.service.Game500Service;
import lombok.Data;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class TeamProcessor implements PageProcessor {

    private Game500Service gameService;

    private String url;
    private String date;
    private String getResult;

    @Override
    public void process(Page page) {
        Document document = page.getHtml().getDocument();
//        Element elementById = document.getElementById(date);
        Elements elements1 = document.select("tbody[id~=^" + date + "]");
        for (Element elementById : elements1) {
            List<Element> elements = new ArrayList<>();
            elements.addAll(elementById.getElementsByClass("vs_lines"));
            elements.addAll(elementById.getElementsByClass("vs_lines even"));
            for (Element element : elements) {
                Game game = new Game();
                //index
                String fid = element.attr("fid");
                game.setIndexId(Long.valueOf(fid));
                //type
                String a = element.getElementsByTag("a").first().text();
                game.setType(a);
                //2022-04-25_473
                String value = element.attr("value");
                String datetime = value.substring(value.indexOf("endTime") + 9, value.indexOf("endTime") + 25) + ":00";
                game.setDate(datetime);
                game.setZhuDui(element.getElementsByTag("a").get(1).text());
                game.setRangQiu(Integer.valueOf(element.getElementsByTag("strong").first().text()));
                game.setKeDui(element.getElementsByTag("a").get(2).text());
                game.setResult(element.getElementsByClass("red eng").text());
                gameService.save(game);
            }
        }
    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
            .addCookie("Cookie", "isagree=1; _jzqy=1.1641812791.1642420657.1.jzqsr=baidu.-; ck_RegFromUrl=https%3A//docs.qq.com/; ck_user_retime=1635743387; _jzqx=1.1641821136.1650628495.12.jzqsr=trade%2E500%2Ecom|jzqct=/.jzqsr=odds%2E500%2Ecom|jzqct=/fenxi/ouzhi-998540%2Eshtml; sdc_session=1650677812948; _jzqc=1; Hm_lvt_4f816d475bb0b9ed640ae412d6b42cab=1650507966,1650594535,1650625763,1650677813; __utmc=63332592; _jzqckmp=1; usercheck=NjU3MDA1ODU5NTVlZGEyOTE4MjhiNGUwYjhhOGZjNWE3NzdiMmYxYw%3D%3D; ck_user=bWYyNTM5NDU%3D; ck_user2=bWYyNTM5NDU%3D; ck_user_utf8=mf253945; ck=NjU3MDA1ODU5NTVlZGEyOTE4MjhiNGUwYjhhOGZjNWE3NzdiMmYxYw%3D%3D; _jzqa=1.3425933191656589300.1641812791.1650677813.1650690911.38; __utma=63332592.69543713.1641812813.1650677814.1650690915.38; __utmz=63332592.1650690915.38.24.utmcsr=500.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _jzqb=1.13.10.1650690911.1; __utmt=1; motion_id=1650692888101_0.025830137988840374; repeat69989f90223482523261fb81b7d63f18=904879fb2f86670eb7b67847326651c1; WT_FPC=id=undefined:lv=1650692890414:ss=1650692880069; sdc_userflag=1650690910887::1650692890416::17; Hm_lpvt_4f816d475bb0b9ed640ae412d6b42cab=1650692890; __utmb=63332592.15.10.1650690915; CLICKSTRN_ID=117.62.7.236-1641812791.481962::D54596D1912700B6AC86F078CA1F0504")
            .setTimeOut(1000000)
            .setRetrySleepTime(10000)
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }

}
